#include "modelo.h"
#include <QFile>
#include <QTextStream>

Modelo::Modelo()
{
}

int Modelo::Load(const QString &file){
    QFile inputFile(file);
    if (inputFile.open(QIODevice::ReadOnly)){
        QTextStream in(&inputFile);
        while ( !in.atEnd() ){
            QString line = in.readLine();
            QStringList values = line.split(" ");
            if(values[0]=="v"){
                Vertice v;
                v.nombre = values[1];
                v.x = values[2].toDouble();
                v.y = values[3].toDouble();
                v.z = values[4].toDouble();
                vertices.append(v);
                vert_transform.append(v);
                vert_transform_map.insert(v.nombre,v);
            }else if(values[0]=="a"){
                Arista a;
                a.nombre = values[1];
                a.v1 = values[2];
                a.v2 = values[3];
                a.cder = values[4];
                a.cizq = values[5];
                a.aizqprev = values[6];
                a.aizqsuc = values[7];
                a.aderprev = values[8];
                a.adersuc = values[9];
                aristas.append(a);
                aristas_map.insert(a.nombre,a);
            }else if(values[0]=="c"){
                Cara c;
                c.nombre = values[1];
                for(int i=2;i<values.count();i++){
                    c.aristas.append(values[i]);
                }
                caras.append(c);
                caras_map.insert(c.nombre,c);
            }else if(values[0]=="color"){
                Color c;
                c.nombre = values[1];
                c.r = values[2].toDouble();
                c.g = values[3].toDouble();
                c.b = values[4].toDouble();

                for(int i=5;i<values.count();i++){
                    c.caras.append(values[i]);
                }
                colores.append(c);
                colors_transform.append(c);
            }
        }
        return 1;
    }

    inputFile.close();
    return 0;
}

bool Modelo::Save(const QString &file){
    QFile output(file);
    if(output.open(QIODevice::WriteOnly)){
        QTextStream out(&output);

        out << "Objeto "+nombre+"\n";
        out << "\n";

        for(int i=0;i<vert_transform.count();i++){
            out<<"v ";
            out<<vert_transform[i].nombre;
            out<<" ";
            double vx = vert_transform[i].x;
            double vy = vert_transform[i].y;
            double vz = vert_transform[i].z;
            out << vx << " " << vy << " " << vz;
            out << "\n";
        }

        for(int i=0;i<aristas.count();i++){
            out<<"a ";
            out<<aristas[i].nombre;
            out<<" ";
            out << aristas[i].v1 << " ";
            out << aristas[i].v2 << " ";
            out << aristas[i].cder << " ";
            out << aristas[i].cizq << " ";
            out << aristas[i].aizqprev << " ";
            out << aristas[i].aizqsuc << " ";
            out << aristas[i].aderprev << " ";
            out << aristas[i].adersuc;
            out << "\n";
        }

        for(int i=0;i<caras.count();i++){
            out<<"c ";
            out<<caras[i].nombre;
            out<<" ";
            for(int j=0;j<caras[i].aristas.count();j++)
                out<<caras[i].aristas[j]<<" ";
            out << "\n";
        }

        for(int i=0;i<colores.count();i++){
            out<<"color ";
            out<<colors_transform[i].nombre;
            out<<" ";
            out<<colors_transform[i].r<<" ";
            out<<colors_transform[i].g<<" ";
            out<<colors_transform[i].b<<" ";
            for(int j=0;j<colors_transform[i].caras.count();j++)
                out<<colors_transform[i].caras[j]<<" ";
            out << "\n";
        }

        return true;
    }

    return false;
}
